www.gusucode.com > Simulink Onramp 工具箱matlab源码程序 > Simulink Onramp/R2018b/web/mw-academy-web/AcademyApp.js

    /**
 * TrainingApp is the main orchestrator, the entry point into the training app.  This will set up the
 *  LRSService, CourseModel (course service), WraDataService, and the two main course views. The services
 *  will in turn make settings to the Remote layer, so that the course view can "just use it"
 */
define("mw-academy-web/AcademyApp", [
    "dojo/_base/declare",
    "dojo/_base/lang",
    "dojo/Deferred",
    "dojo/aspect",
    "dojo/dom",
    "dojo/on",
    "dojo/dom-class",
    "dojo/dom-construct",
    "dojo/dom-attr",
    "dojo/dom-style",
    "dojo/query",
    "dojo/_base/connect",
    "dojo/_base/window",
    "dojo/has",
    "mw-academy-web/wra/WraService",
    "mw-academy-web/wra/WraView",
    "mw-academy-web/courseplayer/CourseReleaseUtil",
    "mw-academy-web/courseplayer/CourseContext",
    "mw-academy-web/courseplayer/CourseModel",
    "mw-academy-web/courseplayer/CourseView",
    "mw-academy-web/lecture/WindowLinkListeners",
    "mw-academy-web/util/queryParams",
    "academyinteractions/MATLABInteraction",
    "mw-academy-web/locationHash/LocationHash",
    "mw-academy-web/udc/UdcRecorderService",
    "mw-academy-web/lrs/LRSService",
    "mw-academy-web/temporarySessionProgress/TemporarySessionProgressService",
    "MOTW/DiscoveryService",
    "MOTW/udc/UdcServiceFactory",
    "MOTW/motw/UnsupportedBrowserCheck",
    "mw-event-utils/BackspaceHandler",
    "MW/Log"
], function(declare, lang, Deferred, aspect, dom, on, dom_class, dom_construct, dom_attr, dom_style, query, connect, dojoWindow, has,
            WraService, WraView, CourseReleaseUtil, CourseContext, CourseModel, CourseView, WindowLinkListeners, queryParams,
            MATLABInteraction, LocationHash, UdcRecorderService, LRSService, TemporarySessionProgressService,
            DiscoveryService, UdcServiceFactory, UnsupportedBrowserCheck, BackspaceHandler, Log){

    return declare([], {

        constructor: function(args) {
            var that = this;

            if (!args) {
                args = {};
            }

            if (args.courseCode) {
                CourseContext.setDefaultCourseCode(args.courseCode);
            }

            if (args.lrsContentPath) {
                CourseContext.setEffectiveLRSContentPath(args.lrsContentPath);
            }

            if (args.courseReleaseStatus){
                CourseReleaseUtil.setCourseReleaseStatus(args.courseReleaseStatus);
            }

            if (args.latestReleaseURL){
                CourseReleaseUtil.setCourseLatestReleaseURL(args.latestReleaseURL);
            }

            this._useAnonymousUdcToken = !!args.useAnonymousLogin;

            //Disable backspace
            BackspaceHandler.disableBackspaceOnNonEditableFields();

            //UDC discovery service
            this.discoveryService = new DiscoveryService();
            on(this.discoveryService, "serviceSet", function (serviceEndPoints) {
                that._setUdcEndpoints(serviceEndPoints);
            });

            //Login views
            this.wraService = new WraService({
                clientType: args.clientType,
                useAnonymousLogin: args.useAnonymousLogin,
                disableSessionTimeout: args.isSimulinkCourse,
                mwEnvironment: args.mwEnvironment,
                matlabLoginFrameworkEndpoint: queryParams.matlabLoginFrameworkEndpoint,
                testUser: queryParams.testUser
            });
            this.wraView = new WraView({
                wraService: this.wraService
            });

            this.lrsService = new LRSService();
            this.lrsService.setWraService(this.wraService.legacyWraService);

            this.temporarySessionProgressService = new TemporarySessionProgressService();


            //Create course model
            this.courseModel = new CourseModel({
                lrsService: this.lrsService,
                temporarySessionProgressService: this.temporarySessionProgressService
            });
            if (this.lrsService) {
                this.lrsService.setCourseModel(this.courseModel);
            }
            this._trackProgress();
            if (args.default_trainingaddon_path) {
                this.courseModel.default_trainingaddon_path = args.default_trainingaddon_path;
            }


            //Create the MATLAB Interaction, unless in a Simulink course
            if (args.isSimulinkCourse) {
                this.matlabInteraction = {
                    isEmpty: true,
                    domNode: null,
                    interactionService: null,
                    load: function(){
                        var deferred = new Deferred();
                        deferred.resolve();
                        return deferred.promise;
                    },
                    setUdcService: function(){}
                };
            } else {
                this.matlabInteraction = new MATLABInteraction({
                    wraService: this.wraService.legacyWraService,
                    discoveryService: this.discoveryService,
                    folderHealthCheck: (CourseContext.getCourseCode() === "mldl") ?
                        (this.courseModel.getTrainingAddonPath() + "AlexNet") : null,
                    initCode: ((CourseContext.getCourseCode() === "deeplearning") || (CourseContext.getCourseCode() === "mldl")) ?
                        "try, rmpath('" + this.courseModel.getTrainingAddonPath() + "AlexNet'), addpath('" + this.courseModel.getTrainingAddonPath() + "AlexNet'), end; alexnet" :
                        null
                });
            }

            this.temporarySessionProgressService.setCourseModel(this.courseModel);

            //locationHash
            this.locationHash = new LocationHash({
                courseModel: this.courseModel
            });

            //Course view
            this.courseView = new CourseView({
                courseModel: this.courseModel,
                wraView: this.wraView,
                matlabInteraction: this.matlabInteraction,
                appArguments: args
            },dom.byId("courseView"));

            //Add window event listener if in Simulink course
            if (args.isSimulinkCourse){
                WindowLinkListeners.addLectureLinkListener();
            }

            //Service listeners
            aspect.after(that.wraService.legacyWraService, "onUpdate", function() {
                try {
                    that._handleStateChange();
                } catch (e) {
                    Log.log(e);
                }
            });
            that.wraService.legacyWraService.onUpdate();

            UnsupportedBrowserCheck.displayDialog();

        },

        _handleStateChange: function () {
            var that = this;

            switch (this.wraService.legacyWraService.getCurrentState()) {
                case "computeResourceNotReady":
                    this.lrsService.reset();
                    this.courseModel.reset();
                    this.stopRecordingToUdc();
                    this.switchToWraView();
                    break;
                case "computeResourceReady":   // always called at completion of authentication (login or reload)
                    this.wraView.setViewToWaiting();
                    that.courseModel.readUserData();
                    if (!this.matlabInteraction.isEmpty) {
                        this.matlabInteraction.load().then(function () {
                            that._doCourseViewSwitch();
                        });
                    } else {
                        that._doCourseViewSwitch();
                    }

                    this._trackOnrampLogin();
                    break;
            }
        },

        _trackOnrampLogin: function () {
            try {
                if (CourseContext.getCourseCode() === "gettingstarted") {
                    _satellite.track("onramp_success");
                }
            } catch (e) {
                //No action required
            }
        },

        _trackProgress: function () {
            var currentProgress = null;
            var hasStarted = false;

            try {
                _satellite.track("mlac_accessed");
            } catch (e) {
                //Noop
            }

            on(this.courseModel, this.courseModel.EVENTS.UPDATE, function () {
                var oldProgress = currentProgress;
                currentProgress = this.courseModel.getCourseProgress();
                //The first update will always be the current state at page load. Subsequent
                // progress updates will allow us to compare, so we skip the first.
                if (!hasStarted) {
                    hasStarted = true;
                } else {
                    this._trackProgressChange(oldProgress,currentProgress);
                }
            }.bind(this));
        },

        _trackProgressChange: function (oldProgress, newProgress) {
            if ((oldProgress === 0) && (newProgress > 0)) {
                try {
                    _satellite.track("mlac_start");
                } catch (e) {
                    //Noop
                }
            }
            if ((oldProgress < 50) && (newProgress >= 50)) {
                try {
                    _satellite.track("mlac_halfway");
                } catch (e) {
                    //Noop
                }
            }
        },

        _doCourseViewSwitch: function () {
            Log.log("Switching to course view");
            var that = this;

            // This sporadically fails at times, and I really have not been able to determine good
            // reproduction steps. So, we set up an automatic retry here.
            try {
                that._setUdcServiceSessionInformation();
                that.startRecordingToUdc();
                that.courseModel.readUserData().always(function () {
                    that.switchToCourseView();
                });
            } catch (e) {
                window.setTimeout(function () {
                    that._doCourseViewSwitch();
                }, 1000);
            }
        },

        _preWraViewSwitch: function () {
            if (this.matlabInteraction.application.layoutManager) {
                //Close all open dialogs
                require("MOTW/dialog/MotwDialogUtils").closeOpenDialogs();
            }
        },


        /*
        * Wra View:
        * Display wraView, display loginForm, hide Courseview
        * */
        switchToWraView: function () {
            this._preWraViewSwitch();
            this.courseModel.changeWraView(this.courseModel.WRA_VIEW_STATES.WRA_VIEW);
            this.courseView.setUserNameLabel("");
        },

        /*
         * Course View:
         * Display Courseview, hide wraView, hide loginForm
         * */
        switchToCourseView: function () {
            var userInfo;

            this.courseModel.changeWraView(this.courseModel.WRA_VIEW_STATES.COURSE_VIEW);
            userInfo = this.wraService.legacyWraService.getCurrentUserInfo();
            if (userInfo.firstName && userInfo.lastName) {
                this.courseView.setUserNameLabel(userInfo.firstName + " " + userInfo.lastName);
            }
            this.courseView.updatePageTitle();
        },


        /**
         * UDC stuff
         */

        _setUdcEndpoints: function(serviceEndPoints) {
            if (serviceEndPoints.USAGE_DATA_COLLECTION_SERVICE && serviceEndPoints.USAGE_DATA_COLLECTION_SERVICE.endpoint) {
                this.UdcService = UdcServiceFactory.getUdcServiceInstance("training",
                    serviceEndPoints.USAGE_DATA_COLLECTION_SERVICE.endpoint);
            } else {
                this.UdcService = new UdcServiceFactory.getUdcNullService();
            }
            this.matlabInteraction.setUdcService(this.UdcService);
        },

        _setUdcServiceSessionInformation: function () {
            if (this.UdcService && this.wraService.legacyWraService.getCurrentUserInfo().userToken) {
                this.UdcService.setSessionCorrelationId(this.wraService.legacyWraService.getCurrentUserInfo().userToken.sessionCorrelationId);
                if (this._useAnonymousUdcToken) {
                    this.UdcService.setMWAToken("2223e83c-c075-4280-92f4-b8078b99d090");
                } else {
                    this.UdcService.setMWAToken(this.wraService.legacyWraService.getCurrentUserInfo().userToken.mwaToken);
                }
            }
        },

        startRecordingToUdc: function () {
            this.stopRecordingToUdc();
            this.udcRecorderService = new UdcRecorderService({
                udcService: this.UdcService,
                courseModel: this.courseModel
            });
            this.udcRecorderService.setInteractionService(this.matlabInteraction.interactionService);
            this.udcRecorderService.logConnection();
        },

        stopRecordingToUdc: function () {
            if (this.udcRecorderService) {
                this.udcRecorderService.destroy();
            }
        }

    });
});